package com.edu.controller;

import com.edu.dao.MenuMapper;
import com.edu.entity.Menu;
import com.edu.entity.Role;
import com.edu.entity.User;
import com.edu.service.MenuService;
import com.edu.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class CommonController {
    @Autowired
    private MenuService menuService;

    //1.登录的视图页面的映射地址
    @RequestMapping("/login")
    public String login(){
        return "views/login2";
    }

    //2.
    @RequestMapping("/index")
    public String index(HttpSession session){
        //1.使用工具类获取当前用户。
        User user = CommonUtils.getLoginUser();
        //2.再获取用户的主角色。
        Role role = user.getPrimaryRole();
        //3.根据角色来获取菜单
        List<Menu> list = menuService.getMenusByUser(role.getId());
        //4.把菜单存入到会话域
        session.setAttribute("menuList",list);
        //5.内部转发到templates/main.html
        return "main";
    }

}
